<?php

class ConnectionRequestTb extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var integer
     */
    public $sender_id;

    /**
     *
     * @var integer
     */
    public $receiver_id;

    /**
     *
     * @var string
     */
    public $date_created;

    /**
     * Independent Column Mapping.
     */
    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'sender_id' => 'sender_id', 
            'receiver_id' => 'receiver_id', 
            'date_created' => 'date_created'
        );
    }

    public function beforeValidationOnCreate()
    {
        $this->date_created = CURR_DATE;
    }
    public function insert($sender_id,$receiver_id)
    {
        $request = new ConnectionRequestTb();
        $request->sender_id = $sender_id;
        $request->receiver_id = $receiver_id;
        return $request->save();
    }
    public function listRequest($receiver_id)
    {
        $phql = "SELECT crt.*,uat.* FROM ConnectionRequestTb crt 
                 LEFT JOIN UserAccountTb uat ON uat.id = crt.sender_id
                 WHERE receiver_id = ?0
                 ORDER BY crt.date_created DESC";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$receiver_id));

        return $data;
    }
}
